package com.gyh.stlserver.controller;

import com.github.pagehelper.Constant;
import com.github.pagehelper.PageInfo;
import com.gyh.stlserver.constants.Constants;
import com.gyh.stlserver.entity.Reserve;
import com.gyh.stlserver.service.CourseService;
import com.gyh.stlserver.service.ReserveService;
import com.gyh.stlserver.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/reserve")
public class ReserveController {
    @Autowired
    private ReserveService reserveService;
    @GetMapping
    public Result getAll(@RequestParam(defaultValue = "1") Integer pageNum,
                         @RequestParam(defaultValue = "10") Integer pageSize,Reserve reserve ){
        PageInfo<Reserve> pageInfo = reserveService.getAll(pageNum,pageSize,reserve);
        return Result.success(Constants.STATUS_200,pageInfo);
    }
    @PostMapping
    public Result add(@RequestBody Reserve reserve ){
        try {
            int i = reserveService.add(reserve);
            if (i<1){
                return Result.error(Constants.STATUS_401,"添加失败");
            }
        }catch (RuntimeException e){
            return Result.error(Constants.STATUS_401,e.getMessage());
        }

        return Result.success(Constants.STATUS_200);
    }
    @PutMapping
    public Result edit(@RequestBody Reserve reserve ){
        try {
            int i = reserveService.edit(reserve);
            if (i<1){
                return Result.error(Constants.STATUS_401,"更新失败");
            }
        }catch (RuntimeException e){
            return Result.error(Constants.STATUS_401,e.getMessage());
        }

        return Result.success(Constants.STATUS_200);
    }
    @GetMapping("/attendance")
    public Result attendance(Integer cid,Integer mid){
        try{
            int i = reserveService.attendance(cid,mid);
            if (i<1){
                return Result.error(Constants.STATUS_401,"签到异常");
            }
        }catch (Exception e){
            return Result.error(Constants.STATUS_401,e.getMessage());
        }
        return Result.success(Constants.STATUS_200);
    }
    @DeleteMapping("/delete")
    public Result delete(Integer mid,Integer cid){
        int i = 0;
        try {
            i = reserveService.removeByCidAndMid(mid,cid);
        }catch (Exception e){
            return Result.error(Constants.STATUS_401,e.getMessage());
        }
        if (i<1){
            return Result.error(Constants.STATUS_401,"删除失败");
        }
        return Result.success(Constants.STATUS_200);
    }
    @PutMapping("/cancel")
    public Result cancel(@RequestBody Reserve reserve){
        int i = 0;
        try {
            i = reserveService.cancelReserve(reserve);
        }catch (Exception e){
            return Result.error(Constants.STATUS_401,e.getMessage());
        }
        if (i<1){
            return Result.error(Constants.STATUS_401,"取消预约失败");
        }
        return Result.success(Constants.STATUS_200);
    }
}
